import service from '/@/utils/request';
import {UploadProgressEvent} from "element-plus";
import {AxiosProgressEvent} from "axios";
import {FileQueryDto} from "/@/types/views";

export const fileApi = {
    listFile: (query?: FileQueryDto) => {
        return service.get<PageResult<FileVo>, FileQueryDto>('/admin/file', {
            params:query
        });
    },
    uploadFile: (uploadFileDto: UploadFileDto, progress?: (evt: UploadProgressEvent) => void) => {
        return service.upload<FileVo, UploadFileDto>('/admin/file/uploadFile', uploadFileDto, {
            onUploadProgress: (progressEvent: AxiosProgressEvent) => {
                let percent = Math.round((progressEvent.loaded * 100) / progressEvent.total!)
                if (progress) {
                    progress({
                        percent: percent,
                        ...progressEvent.event
                    });
                }
            },
        });
    },
    uploadFiles(file: BatchUploadFileDto) {
        return service.upload<BatchUploadFileVo, BatchUploadFileDto>('/admin/file/uploadFiles', file, {});
    },
    uploadByUrl(file: UploadByUrlDto) {
        return service.post<UploadByUrlVo, UploadByUrlDto>('/admin/file/uploadByUrl', file);
    },
    deleteFile(fileId: number) {
        return service.delete<boolean>(`/admin/file/${fileId}`, {
            params: {
                id: fileId
            }
        });
    },
    getFile(fileId: number) {
        return service.get<FileVo>(`/admin/file/${fileId}`);
    }
}